package com.bytedance.common.plugin.interfaces.pushmanager;

import android.text.TextUtils;

import com.bytedance.common.plugin.interfaces.pushmanager.setting.PushSetting;
import com.bytedance.common.utility.Logger;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.HashSet;

public class PushChannelHelper {
    public static final int ALIYUN_PUSH = 9;
    public static final int GCM_PUSH = 5;
    public static final int GETUI_PUSH = 4;
    public static final int HW_PUSH = 7;
    public static final int IXINTUI_PUSH = 3;
    public static final int MI_PUSH = 1;
    public static final int MYSELF_PUSH = 2;
    public static final int MZ_PUSH = 8;
    public static final int OPPO_PUSH = 10;
    private static final String TAG = "PushChannelHelper";
    public static final int UMENG_PUSH = 6;
    private static final int UNREGISTER_ALL_SENDER = -9307;
    private boolean mAliyunPushInclude;
    private static final HashSet mAllowPushSet;
    private boolean mHwPushInclude;
    private boolean mMiPushInclude;
    private boolean mMyPushInclude;
    private boolean mMzPushInclude;
    private boolean mOppoPushInclude;
    private boolean mUmengPushInclude;
    private static PushChannelHelper sPushChannelHelper;

    static {
        sPushChannelHelper = new PushChannelHelper();
        mAllowPushSet = new HashSet();
    }

    private PushChannelHelper() {

        this.mMiPushInclude = true;
        this.mMyPushInclude = true;
        this.mUmengPushInclude = true;
        this.mHwPushInclude = true;
        this.mMzPushInclude = true;
        this.mAliyunPushInclude = true;
        this.mOppoPushInclude = false;
    }

    public JSONArray buildApplogHeader() {
        JSONArray v0 = new JSONArray();
        if (this.mMiPushInclude) {
            v0.put(1);
        }

        if (this.mMyPushInclude) {
            v0.put(2);
        }

        if (this.mUmengPushInclude) {
            v0.put(6);
        }

        if (this.mHwPushInclude) {
            v0.put(7);
        }

        if (this.mMzPushInclude) {
            v0.put(8);
        }

        if (this.mAliyunPushInclude) {
            v0.put(9);
        }

        if (this.mOppoPushInclude) {
            v0.put(10);
        }

        return v0;
    }

    public static PushChannelHelper getInstance() {
        return sPushChannelHelper;
    }

    public static void handlerApplogConfig(String arg7) {
        JSONArray v2 = null;
        int v1 = 0;
        PushSetting.getInstance().setPushChannelsJsonArray(arg7);
        if (!TextUtils.isEmpty(arg7)) {
            try {
                v2 = new JSONArray(arg7);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }

            if (v2 == null) {
                return;
            }

            if (Logger.debug()) {
                Logger.d("PushChannelHelper", "handlerApplogConfig: jsonArray = " + v2);
            }

            mAllowPushSet.clear();
            int v3 = v2.length();
            boolean v0_1 = false;
            while (v1 < v3) {
                int v4 = v2.optInt(v1);
                if (v4 > 0) {
                    mAllowPushSet.add(Integer.valueOf(v4));
                    if (v4 == 2) {
                        v0_1 = true;
                    }
                }

                ++v1;
            }

            if (Logger.debug()) {
                Logger.d("PushChannelHelper", "handlerApplogConfig: mAllowPushSet = " + mAllowPushSet);
            }

            PushSetting.getInstance().setAllowSelfPushEnable(v0_1);
        }
    }

    public boolean hasSupportChannel(String arg7) {
        int v2;
        boolean v0 = true;
        try {
            if ((TextUtils.isEmpty(((CharSequence) arg7))) || ("[]".equals(arg7))) {
                return false;
            }

            JSONArray v3 = new JSONArray(arg7);
            if (v3.length() <= 0) {
                return false;
            }

            if (v3.optInt(0) == -9307) {
                return v0;
            }

            String v4 = getInstance().buildApplogHeader().toString();
            v2 = 0;
            while (true) {
                if (v2 >= v3.length()) {
                    return false;
                }

                if (v4.contains(String.valueOf(v3.optInt(v2)))) {
                    return v0;
                }

                ++v2;
                continue;
            }
        } catch (JSONException v0_1) {
            v0_1.printStackTrace();
        }

        return false;
    }

    private static void initAllowPushSet() {
        if (Logger.debug()) {
            Logger.d("PushChannelHelper", "initAllowPushSet: mAllowPushSet = " + mAllowPushSet);
        }

        if (PushChannelHelper.mAllowPushSet.isEmpty()) {
            handlerApplogConfig(PushSetting.getInstance().getPushChannelsJsonArray());
        }
    }

    public static boolean isAliYunPushAvailable() {
        int v2 = 9;
        initAllowPushSet();
        boolean v0 = !PushChannelHelper.mAllowPushSet.contains(Integer.valueOf(v2)) || !PushChannelHelper.mAllowPushSet.contains(Integer.valueOf(6)) ? mAllowPushSet.contains(Integer.valueOf(v2)) : false;
        return v0;
    }

    public boolean isAliyunPushInclude() {
        return this.mAliyunPushInclude;
    }

    public static boolean isHWPushAvailable() {
        boolean v0 = mAllowPushSet.contains(Integer.valueOf(7)) ? true : false;
        return v0;
    }

    public boolean isHwPushInclude() {
        return this.mHwPushInclude;
    }

    public static boolean isMZPushAvailable() {
        initAllowPushSet();
        boolean v0 = mAllowPushSet.contains(Integer.valueOf(8)) ? true : false;
        return v0;
    }

    public static boolean isMiPushAvailable() {
        boolean v0 = true;
        initAllowPushSet();
        if (!PushChannelHelper.mAllowPushSet.contains(Integer.valueOf(1))) {
            v0 = false;
        }

        return v0;
    }

    public boolean isMiPushInclude() {
        return this.mMiPushInclude;
    }

    public boolean isMyPushInclude() {
        return this.mMyPushInclude;
    }

    public static boolean isMySelfPushAvailable() {
        initAllowPushSet();
        boolean v0 = mAllowPushSet.contains(Integer.valueOf(2)) ? true : false;
        return v0;
    }

    public boolean isMzPushInclude() {
        return this.mMzPushInclude;
    }

    public static boolean isOppoPushAvailable() {
        initAllowPushSet();
        boolean v0 = mAllowPushSet.contains(Integer.valueOf(10)) ? true : false;
        return v0;
    }

    public boolean isOppoPushInclude() {
        return this.mOppoPushInclude;
    }

    public static boolean isPushAvailable(int arg2) {
        boolean v0;
        initAllowPushSet();
        if (arg2 == 9) {
            v0 = isAliYunPushAvailable();
        } else if (PushChannelHelper.mAllowPushSet.contains(Integer.valueOf(arg2))) {
            v0 = true;
        } else {
            v0 = false;
        }

        return v0;
    }

    public static boolean isUmengPushAvailable() {
        initAllowPushSet();
        boolean v0 = mAllowPushSet.contains(Integer.valueOf(6)) ? true : false;
        return v0;
    }

    public boolean isUmengPushInclude() {
        return this.mUmengPushInclude;
    }

    public PushChannelHelper onThirdPushDexLoadFailed() {
        this.mMiPushInclude = false;
        this.mMyPushInclude = true;
        this.mUmengPushInclude = false;
        this.mHwPushInclude = false;
        this.mMzPushInclude = false;
        this.mAliyunPushInclude = false;
        this.mOppoPushInclude = false;
        return this;
    }

    public PushChannelHelper setAliyunPushInclude(boolean arg1) {
        this.mAliyunPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setHwPushInclude(boolean arg1) {
        this.mHwPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setMiPushInclude(boolean arg1) {
        this.mMiPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setMyPushInclude(boolean arg1) {
        this.mMyPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setMzPushInclude(boolean arg1) {
        this.mMzPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setOppoPushInclude(boolean arg1) {
        this.mOppoPushInclude = arg1;
        return this;
    }

    public PushChannelHelper setUmengPushInclude(boolean arg1) {
        this.mUmengPushInclude = arg1;
        return this;
    }
}

